﻿using NUnit.Framework;
using Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Models;
using System;
using System.IO;
using System.Linq;

namespace Sasa.QualityTools.CodeAnalysis.FxCopIntegrator.Tests.Unit.Models
{
    [TestFixture]
    public class CodeAnalysisConfigurationTest
    {
        [Test]
        public void TestLoadWithConfig1_2_0()
        {
            var contents = new string[] {
                @"<?xml version=""1.0"" encoding=""utf-8""?>",
                @"<CodeAnalysisConfiguration Version=""1.2.0"" Enable=""true"">",
                @"    <EnableSearchGAC Override=""true"" Value=""false"" />",
                @"    <IgnoreGeneratedCode Override=""true"" Value=""false"" />",
                @"    <Timeout Override=""true"" Value=""60"" />",
                @"    <RuleSet Override=""true"" Value=""Bundled;all.ruleset"" />",
                @"</CodeAnalysisConfiguration>" };

            var testee = CreateCodeAnalysisConfiguration(contents);
            Assert.AreEqual(testee.EnableCodeAnalysis, true);
            Assert.AreEqual(testee.OverrideEnableSearchGac, true);
            Assert.AreEqual(testee.OverrideIgnoreGeneratedCode, true);
            Assert.AreEqual(testee.OverrideTimeout, true);
            Assert.AreEqual(testee.OverrideRuleSet, true);
            Assert.AreEqual(testee.OverrideEnableCodeAnalysisOnBuild, false);
            Assert.AreEqual(testee.EnableSearchGac, false);
            Assert.AreEqual(testee.IgnoreGeneratedCode, false);
            Assert.AreEqual(testee.Timeout, 60);
            Assert.AreEqual(testee.RuleSet, GetRuleSetPath("all.ruleset"));
            Assert.AreEqual(testee.EnableCodeAnalysisOnBuild, false);
        }

        [Test]
        public void TestLoadWithConfig1_3_0()
        {
            var contents = new string[] {
                @"<?xml version=""1.0"" encoding=""utf-8""?>",
                @"<CodeAnalysisConfiguration Version=""1.3.0"" Enable=""true"">",
                @"    <EnableSearchGAC Override=""true"" Value=""false"" />",
                @"    <IgnoreGeneratedCode Override=""true"" Value=""false"" />",
                @"    <Timeout Override=""true"" Value=""60"" />",
                @"    <RuleSet Override=""true"" Value=""Bundled;all.ruleset"" />",
                @"    <EnableCodeAnalysisOnBuild Override=""true"" Value=""true"" />",
                @"</CodeAnalysisConfiguration>" };

            var testee = CreateCodeAnalysisConfiguration(contents);
            Assert.AreEqual(testee.EnableCodeAnalysis, true);
            Assert.AreEqual(testee.OverrideEnableSearchGac, true);
            Assert.AreEqual(testee.OverrideIgnoreGeneratedCode, true);
            Assert.AreEqual(testee.OverrideTimeout, true);
            Assert.AreEqual(testee.OverrideRuleSet, true);
            Assert.AreEqual(testee.OverrideEnableCodeAnalysisOnBuild, true);
            Assert.AreEqual(testee.EnableSearchGac, false);
            Assert.AreEqual(testee.IgnoreGeneratedCode, false);
            Assert.AreEqual(testee.Timeout, 60);
            Assert.AreEqual(testee.RuleSet, GetRuleSetPath("all.ruleset"));
            Assert.AreEqual(testee.EnableCodeAnalysisOnBuild, true);
        }

        [Test]
        public void TestSave()
        {
            var path = GetType().Name + ".xml";
            var testee = new CodeAnalysisConfiguration(path);
            testee.EnableCodeAnalysis = false;
            testee.OverrideEnableSearchGac = false;
            testee.OverrideIgnoreGeneratedCode = false;
            testee.OverrideTimeout = false;
            testee.OverrideRuleSet = false;
            testee.OverrideEnableCodeAnalysisOnBuild = false;
            testee.EnableSearchGac = false;
            testee.IgnoreGeneratedCode = false;
            testee.Timeout = 60;
            testee.RuleSet = GetRuleSetPath("all.ruleset");
            testee.EnableCodeAnalysisOnBuild = true;

            testee.Save();
            var actual = File.ReadAllLines(path);

            var expected = new string[] {
                @"<?xml version=""1.0"" encoding=""utf-8""?>",
                @"<CodeAnalysisConfiguration Version=""1.3.0"" Enable=""false"">",
                @"    <EnableSearchGAC Override=""false"" Value=""false"" />",
                @"    <IgnoreGeneratedCode Override=""false"" Value=""false"" />",
                @"    <Timeout Override=""false"" Value=""60"" />",
                @"    <RuleSet Override=""false"" Value=""Bundled;all.ruleset"" />",
                @"    <EnableCodeAnalysisOnBuild Override=""false"" Value=""true"" />",
                @"</CodeAnalysisConfiguration>" };

            CollectionAssert.AreEqual(expected.Select(o => o.Trim()).ToList(), actual.Select(o => o.Trim()).ToList());
        }

        private CodeAnalysisConfiguration CreateCodeAnalysisConfiguration(params string[] contents)
        {
            var path = GetType().Name + ".xml";
            File.WriteAllLines(path, contents);
            return CodeAnalysisConfiguration.Load(path);
        }

        private string GetRuleSetPath(string fileName)
        {
            return Path.Combine(Environment.GetEnvironmentVariable("VS100COMNTOOLS"), "..", "..", "Team Tools", "Static Analysis Tools", "Rule Sets", fileName);
        }

        private string GetRuleSetPath(string configurationPath, string ruleSetFileName)
        {
            return Path.Combine(Path.GetDirectoryName(configurationPath), ruleSetFileName);
        }
    }
}
